package com.soilflow.info.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soilflow.common.core.redis.RedisCache;
import com.soilflow.common.domain.DahuBusiness;
import com.soilflow.common.domain.DahuConvenient;
import com.soilflow.common.domain.dto.DahuConvenientDto;
import com.soilflow.info.mapper.DahuConvenientMapper;
import com.soilflow.info.service.IDahuConvenientService;
import com.soilflow.system.mapper.DahuBusinessMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class DahuConvenientServiceimpl extends ServiceImpl<DahuConvenientMapper, DahuConvenient> implements IDahuConvenientService {
    @Resource
    private DahuConvenientMapper convenientMapper;
    @Resource
    private DahuBusinessMapper businessMapper;
    @Autowired
    private RedisCache redisCache;

    @Override
    public List<DahuConvenientDto> selectConvenientList(DahuConvenientDto dto) {
        List<DahuConvenientDto> dahuConvenientDtos = convenientMapper.selectConvenientList(dto);
        for (DahuConvenientDto dahuConvenientDto : dahuConvenientDtos) {
            Long createId = dahuConvenientDto.getCreateId();
            DahuBusiness business = businessMapper.selectById(createId);
            dahuConvenientDto.setBusiness(business);
        }
        return dahuConvenientDtos;
    }

    @Override
    public int updateViewCount(Long id) {
        redisCache.incrementCacheMapValue("DahuConvenient:number",id.toString(),1);
        return 1;
    }

    @Override
    public DahuConvenientDto getConvenient(Long id) {
        LambdaQueryWrapper<DahuConvenient> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(DahuConvenient::getId,id);
        DahuConvenient byId = this.getById(id);
        DahuBusiness business = businessMapper.selectById(byId.getCreateId());
        DahuConvenientDto dto=new DahuConvenientDto();
        BeanUtils.copyProperties(byId,dto);
        dto.setBusiness(business);
        return dto;
    }

    @Override
    public boolean add(DahuConvenient convenient) {
        //TODO 获取当前用户id
        convenient.setUpdateTime(new Date());
       return this.save(convenient);
    }
}
